package com.crm.business.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.crm.business.crm.service.*;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.entity.BO.CrmContractSaveBO;
import com.crm.common.crm.entity.BO.CrmModelSaveBO;
import com.crm.common.crm.entity.PO.*;
import com.crm.common.crm.entity.VO.CrmModelFiledVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("crmDealStatusService")
public class ICrmDealStatusServiceImpl implements ICrmDealStatusService, CrmPageService {

    @Autowired
    private ICrmContractService crmContractService;
    @Autowired
    private ICrmContractDataService crmContractDataService;
    @Autowired
    private ICrmCustomerVipService crmCustomerService;
    @Autowired
    private ICrmCustomerDataService crmCustomerDataService;
    @Autowired
    private ICrmLeadsService crmLeadsService;
    @Autowired
    private ICrmLeadsDataService iCrmLeadsDataService;



    public boolean update(){
        try {
            List<CrmContract> contractList = crmContractService.lambdaQuery().eq(CrmContract::getDealStatus, "未成交").ge(CrmContract::getReceivedMoney, 500).list();
            if (!CollectionUtils.isEmpty(contractList)){
                for (CrmContract contract:contractList){

                    CrmCustomer customer = crmCustomerService.lambdaQuery().eq(CrmCustomer::getCustomerId, contract.getCustomerId()).one();
                    CrmLeads leads = crmLeadsService.lambdaQuery().eq(CrmLeads::getSerialNumber, customer.getSerialNumber()).one();
//                    CrmLeadsData leadsData = iCrmLeadsDataService.lambdaQuery().eq(CrmLeadsData::getBatchId, leads.getBatchId()).eq(CrmLeadsData::getName, "fliedIlvojx").one();
                    List<CrmLeadsData> dataList = iCrmLeadsDataService.lambdaQuery().eq(CrmLeadsData::getBatchId, leads.getBatchId()).list();
                    List<CrmCustomerData> list_data_customer = crmCustomerDataService.lambdaQuery().eq(CrmCustomerData::getBatchId, customer.getBatchId()).list();
                    List<CrmContractData> list_data_contract = crmContractDataService.lambdaQuery().eq(CrmContractData::getBatchId, contract.getBatchId()).list();
                    boolean a=true;
                    boolean b=true;
                    if (!StringUtils.isEmpty(customer.getTelephone())){
                        a = customer.getTelephone().equals(leads.getTelephone());
                    }

                    if (!StringUtils.isEmpty(customer.getWechat())){
                        b=  customer.getWechat().equals(leads.getWechat());
                    }
                    if (a||b){
                        crmLeadsService.lambdaUpdate().set(CrmLeads::getVipDealStatus, "已成交").set(CrmLeads::getVipDealTime, contract.getRecentBackTime()).eq(CrmLeads::getLeadsId, leads.getLeadsId()).update();
                        leads = crmLeadsService.lambdaQuery().eq(CrmLeads::getSerialNumber, customer.getSerialNumber()).one();
                        //更新线索ES
                        updateEsDataLeads(leads,dataList);



                        crmCustomerService.lambdaUpdate().set(CrmCustomer::getVipDealStatus,"已成交").set(CrmCustomer::getVipDealTime,contract.getRecentBackTime()).eq(CrmCustomer::getCustomerId,customer.getCustomerId()).update();
                        customer = crmCustomerService.lambdaQuery().eq(CrmCustomer::getCustomerId, contract.getCustomerId()).one();
                        //更新客户ES
                        updateEsDataCustomer(customer,list_data_customer);


                        crmContractService.lambdaUpdate().set(CrmContract::getDealStatus,"已成交").set(CrmContract::getDealTime,contract.getRecentBackTime()).eq(CrmContract::getContractId,contract.getContractId()).update();
                        CrmContract contract1 = crmContractService.lambdaQuery().eq(CrmContract::getContractId, contract.getContractId()).one();
                        //更新合同ES
                        updateEsDataContract(contract1,list_data_contract);
                    }



                }

            }
              return true;
        }catch (Exception e){
            e.printStackTrace();
        }


        return false;
    }



         public  void updateEsDataLeads( CrmLeads leads , List<CrmLeadsData> dataList){
             CrmModelSaveBO crmModelSaveBO =new CrmModelSaveBO();
             crmModelSaveBO.setEntity(BeanUtil.beanToMap(leads));
             List<CrmModelFiledVO> crmModelFiledVOList = new ArrayList<>();
             for(int i=0;i<dataList.size();i++){
                 CrmModelFiledVO c = new CrmModelFiledVO();
                 c.setFieldId(dataList.get(i).getFieldId());
                 c.setValue(dataList.get(i).getValue());
                 c.setFieldName(dataList.get(i).getName());
                 crmModelFiledVOList.add(c);
             }
             crmModelSaveBO.setField(crmModelFiledVOList);
             this.savePageStaus(crmModelSaveBO,leads.getLeadsId(),false,1);

         }

    public  void updateEsDataCustomer( CrmCustomer customer, List<CrmCustomerData> dataList){
        CrmModelSaveBO crmModelSaveBO =new CrmModelSaveBO();
        crmModelSaveBO.setEntity(BeanUtil.beanToMap(customer));
        List<CrmModelFiledVO> crmModelFiledVOList = new ArrayList<>();
        for(int i=0;i<dataList.size();i++){
            CrmModelFiledVO c = new CrmModelFiledVO();
            c.setFieldId(dataList.get(i).getFieldId());
            c.setValue(dataList.get(i).getValue());
            c.setFieldName(dataList.get(i).getName());
            crmModelFiledVOList.add(c);
        }
        crmModelSaveBO.setField(crmModelFiledVOList);
        this.savePageStaus(crmModelSaveBO,customer.getCustomerId(),false,2);

    }

    public  void updateEsDataContract(CrmContract crmContract, List<CrmContractData> dataList){
        CrmContractSaveBO crmModelSaveBO =new CrmContractSaveBO();
        crmModelSaveBO.setEntity(BeanUtil.beanToMap(crmContract));
        List<CrmModelFiledVO> crmModelFiledVOList = new ArrayList<>();
        for(int i=0;i<dataList.size();i++){
            CrmModelFiledVO c = new CrmModelFiledVO();
            c.setFieldId(dataList.get(i).getFieldId());
            c.setValue(dataList.get(i).getValue());
            c.setFieldName(dataList.get(i).getName());
            crmModelFiledVOList.add(c);
        }
        crmModelSaveBO.setField(crmModelFiledVOList);
        this.savePageStaus(crmModelSaveBO,crmContract.getContractId(),false,3);


    }


    @Override
    public String[] appendSearch() {
        return new String[0];
    }

    @Override
    public void setOtherField(Map<String, Object> map) {

    }

    @Override
    public CrmEnum getLabel() {
        return null;
    }

    @Override
    public List<CrmModelFiledVO> queryDefaultField() {
        return null;
    }
}
